package com.xmindguoguo.boot.modular.system.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.xmindguoguo.boot.common.annotation.OperationLog;
import com.xmindguoguo.boot.common.constant.GlobalConstant;
import com.xmindguoguo.boot.common.enumtype.OperationLogType;
import com.xmindguoguo.boot.core.BaseController;
import com.xmindguoguo.boot.core.shiro.ShiroKit;
import com.xmindguoguo.boot.modular.system.model.TSystemMenuModel;
import com.xmindguoguo.boot.modular.system.service.ITSystemMenuService;
import com.xmindguoguo.boot.util.CmUtil;
import com.xmindguoguo.boot.util.ConvertUtil;
import com.xmindguoguo.boot.util.ToolUtil;
import com.xmindguoguo.boot.util.json.JsonResult;

/**
 * 控制器 //先暂时全部小写，后面写个方法注册进来，调整大小写
 * 
 * @author xmind-果果
 * @Date 2018-12-10 17:09
 */

@Controller
@RequestMapping("/tSystemMenu")
public class TSystemMenuController extends BaseController {

    // 所属子模块名称+当前功能名称
    private String PREFIX = "/system/tSystemMenu/";

    @Autowired
    private ITSystemMenuService systemMenuService;

    /**
     * 跳转列表页
     */
    @RequestMapping("toTSystemMenuList")
    public String toTSystemMenuList() {
        return PREFIX + "tSystemMenuList";
    }

    /**
     * 查询列表数据 加上分页
     */
    @RequestMapping("tSystemMenuList")
    @ResponseBody
    public Object tSystemMenuList(TSystemMenuModel model) {
        setAttr();
        Wrapper<TSystemMenuModel> wrapper = new EntityWrapper<>();
        if (ToolUtil.isNotEmpty(model.getName())) {
            wrapper.like("name", model.getName());
        }
        if (ToolUtil.isNotEmpty(model.getReqUrl())) {
            wrapper.like("reqUrl", model.getReqUrl());
        }
        wrapper.ne("status", GlobalConstant.MODEL_DEL);
        wrapper.orderBy("orderNum ", false);
        wrapper.orderBy("createTime ", false);
        return JsonResult.sendOk(systemMenuService.selectList(wrapper));
    }

    @RequestMapping("toTSystemMenuAdd")
    public String toTSystemMenuAdd() {
        return PREFIX + "tSystemMenuAdd";
    }

    /**
     * 添加model
     */
    @OperationLog(logName = "添加权限菜单", operationLogType = OperationLogType.INSERT)
    @RequestMapping("tSystemMenuAdd")
    @ResponseBody
    public Object tSystemMenuAdd(@Validated TSystemMenuModel model, BindingResult result) {
        if (result.hasErrors()) {
            return sendValidatedMsg(result);
        }
        model.setId(CmUtil.getPkId());
        model.setStatus(GlobalConstant.MODEL_ADD);
        model.setCreateTime(new Date());
        model.setCreateUserId(ShiroKit.getUserId());
        if (model.getPId() == null) {
            model.setPId(0L);
            model.setPIds(model.getId() + "");
        }
        if (model.getPId() != 0L) {
            TSystemMenuModel oldModel = systemMenuService.selectById(model.getPId());
            model.setPIds(oldModel.getPIds() + "_" + model.getId());
        }
        boolean flag = systemMenuService.insert(model);
        return flag ? JsonResult.sendOk("添加成功", null) : JsonResult.sendError("添加失败", null);
    }

    @RequestMapping("toTSystemMenuUpdate")
    public String toTSystemMenuUpdate(Long id) {
        TSystemMenuModel model = systemMenuService.selectById(id);
        request.setAttribute("model", model);
        // 查询出来对应的上级部门名称
        if (model != null && model.getPId() != 0) {
            TSystemMenuModel pModel = systemMenuService.selectById(model.getPId());
            request.setAttribute("showNameDom", pModel.getName());
        } else {
            request.setAttribute("showNameDom", "顶级");
        }
        return PREFIX + "tSystemMenuUpdate";
    }

    /**
     * 更新model
     */
    @OperationLog(logName = "更新权限菜单", operationLogType = OperationLogType.UPDATE)
    @RequestMapping("tSystemMenuUpdate")
    @ResponseBody
    public Object tSystemMenuUpdate(@Validated TSystemMenuModel model, BindingResult result) {
        if (result.hasErrors()) {
            return sendValidatedMsg(result);
        }
        model.setUpdateTime(new Date());
        model.setUpdateUserId(ShiroKit.getUserId());
        if (model.getPId() == null) {
            model.setPId(0L);
            model.setPIds(model.getId() + "");
        }
        if (model.getPId() != 0L) {
            TSystemMenuModel oldModel = systemMenuService.selectById(model.getPId());
            model.setPIds(oldModel.getPIds() + "_" + model.getId());
        }
        boolean flag = systemMenuService.updateById(model);
        return flag ? JsonResult.sendOk("更新成功", null) : JsonResult.sendError("更新失败", null);
    }

    @OperationLog(logName = "删除权限菜单", operationLogType = OperationLogType.DELETE)
    @RequestMapping("tSystemMenuDelete")
    @ResponseBody
    public Object tSystemMenuDelete(Long id) {
        TSystemMenuModel model = new TSystemMenuModel();
        model.setStatus(GlobalConstant.MODEL_DEL);
        model.setId(id);
        boolean flag = systemMenuService.updateById(model);
        return flag ? JsonResult.sendOk("删除成功", null) : JsonResult.sendError("删除失败", null);
    }

    @OperationLog(logName = "批量删除权限菜单", operationLogType = OperationLogType.DELETE)
    @RequestMapping("tSystemMenuBatchDel")
    @ResponseBody
    public Object tSystemMenuBatchDel(String ids) {
        Long[] idsArray = ConvertUtil.StringToLongArray(ids);
        List<TSystemMenuModel> list = new ArrayList<>();
        TSystemMenuModel model = null;
        for (Long id : idsArray) {
            model = new TSystemMenuModel();
            model.setStatus(GlobalConstant.MODEL_DEL);
            model.setId(id);
            list.add(model);
        }
        boolean flag = systemMenuService.updateBatchById(list);
        return flag ? JsonResult.sendOk("删除成功", null) : JsonResult.sendError("删除失败", null);
    }

}